package com.tyrant.houtai;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.sunshine.retrofit.HttpUtil;
import com.sunshine.retrofit.interfaces.ErrorFunListener;
import com.sunshine.retrofit.interfaces.Success;
import com.tyrant.houtai.bean.RegisterInfoBean;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterUserActivity extends AppCompatActivity {

    @Bind(R.id.et_phone)
    EditText et_phone;
    @Bind(R.id.tv_state)
    TextView tv_state;
    @Bind(R.id.btn_query)
    Button btn_query;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_user);
        ButterKnife.bind(this);
    }

    private void queryinfo() {
        new HttpUtil.Builder("users/findUserByMobile")
                .Params("mobile", et_phone.getText().toString())
                .Tag(this)//需要取消请求的tag
                .Success(new Success() {
                    @Override
                    public void Success(String model) {
                        RegisterInfoBean contactUsBean = new Gson().fromJson(model, RegisterInfoBean.class);
                        if (contactUsBean.getFlag() == 1) {
                            String authorization_state = contactUsBean.getResult().getAuthorization_state();
                            String state;
                            if (authorization_state.equals("0")) {
                                state = "未输入授权码";

                            }else{
                                state = "网络未开通";

                            }
                            tv_state.setText("用户名:"+contactUsBean.getResult().getUsername()+"\n"+ "密码: "+contactUsBean.getResult().getPassword()+"\n "+" 状态: "+state);
                        } else if (contactUsBean.getFlag() == 2) {

                            Toast.makeText(RegisterUserActivity.this, "不存在该用户", Toast.LENGTH_SHORT).show();

                        } else {
                            Toast.makeText(RegisterUserActivity.this, contactUsBean.getMsg(), Toast.LENGTH_SHORT).show();

                        }
                    }
                }).ErrorEventFunc(new ErrorFunListener() {
            @Override
            public void ErrorFun(Object... values) {
                String value = (String) values[1];
            }
        })
                .post();
    }

    @OnClick(R.id.btn_query)
    public void onClick() {
        queryinfo();

    }
}
